﻿using System;
using System.Collections.Generic;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core.Authority;

namespace HMIS.Infrastructure.Core.Repositories.Impl
{
    public class RoleRepositoryImpl : RepositoryNhbImpl<Role>, IRoleRepository
    {

        #region IRoleRepository 成员

		public IList<Role> GetPlist(int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// 排序
			string ordStr = string.IsNullOrEmpty(sort) ? string.Empty : "order by t." + sort + " " + dir;
			// 筛选
			string strFilter = base.GetHqlstrByExtFilter(filters, "t");//通过刚才基类的方法把filters转换成HQL字符串
			// sqlWhere
			string strSqlWhe = @" from Role as t " + strFilter;
			// 查询
			var q = Session.CreateQuery(@"select t " + strSqlWhe + ordStr);
			// 分页
			if (limit > 0)
				q.SetFirstResult(start).SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) " + strSqlWhe).UniqueResult<long>();
			return q.List<Role>();
		}

        public IList<Role> GetPlistAll(int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
        {
            var strFilter = base.GetHqlstrByExtFilter(filters, "r");
            sort = "r." + sort;

            var query = Session.CreateQuery(@"select r from Role as r"
                    + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter)
                    + " order by " + sort + " " + dir)
                    .SetFirstResult(start)
                    .SetMaxResults(limit);

            total = Session.CreateQuery(@"select count(*) from Role as r"
                 + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter))
                .UniqueResult<long>();

            return query.List<Role>();
        }
        public bool IsFieldExist(string fieldName, string fieldValue, string id)
        {
            return base.IsFieldExist(fieldName, fieldValue, id, null);
        }
        public IList<Role> QueryPlist(string query, int start, int limit, out long total)
        {
            var q = Session.CreateQuery(@"select s from Role as s
                                                    where lower(s.Name) like lower(:query)
                                                    or lower(s.Code) like lower(:query)
                                                ")
                        .SetString("query", "%" + query + "%")
                        .SetFirstResult(start)
                        .SetMaxResults(limit);

            total = Session.CreateQuery(@"select count(*) from Role as s
                                                    where lower(s.Name) like lower(:query)
                                                    or lower(s.Code) like lower(:query)
                                                    ")
                   .SetString("query", "%" + query + "%")
                   .UniqueResult<long>();

            return q.List<Role>();
        }

        public Role GetByIdWithMenuAndPermissions(string id)
        {
            try
            {
                var query = Session.CreateQuery(@"select distinct t from Role as t
                                                    left join fetch t.MenuNodes
                                                    left join fetch t.ActionPermissions
                                                   where t.Id=:Id");
                query.SetString("Id", id);

                IList<Role> result = query.List<Role>();

                if (result.Count <= 0)
                    throw new RepositoryException("返回实体为空");
                else
                    return result[0];
            }
            catch (Exception ex)
            {
                throw new RepositoryException("获取实体失败", ex);
            }
        }
        #endregion
    }
}
